package cn.ctodb.mall.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.mall.service.BackGoodsService;
import cn.ctodb.mall.web.rest.util.HeaderUtil;
import cn.ctodb.mall.service.dto.BackGoodsDTO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing BackGoods.
 */
@RestController
@RequestMapping("/api")
public class BackGoodsResource {

    private final Logger log = LoggerFactory.getLogger(BackGoodsResource.class);
        
    @Inject
    private BackGoodsService backGoodsService;

    /**
     * POST  /back-goods : Create a new backGoods.
     *
     * @param backGoodsDTO the backGoodsDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new backGoodsDTO, or with status 400 (Bad Request) if the backGoods has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/back-goods")
    @Timed
    public ResponseEntity<BackGoodsDTO> createBackGoods(@RequestBody BackGoodsDTO backGoodsDTO) throws URISyntaxException {
        log.debug("REST request to save BackGoods : {}", backGoodsDTO);
        if (backGoodsDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("backGoods", "idexists", "A new backGoods cannot already have an ID")).body(null);
        }
        BackGoodsDTO result = backGoodsService.save(backGoodsDTO);
        return ResponseEntity.created(new URI("/api/back-goods/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("backGoods", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /back-goods : Updates an existing backGoods.
     *
     * @param backGoodsDTO the backGoodsDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated backGoodsDTO,
     * or with status 400 (Bad Request) if the backGoodsDTO is not valid,
     * or with status 500 (Internal Server Error) if the backGoodsDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/back-goods")
    @Timed
    public ResponseEntity<BackGoodsDTO> updateBackGoods(@RequestBody BackGoodsDTO backGoodsDTO) throws URISyntaxException {
        log.debug("REST request to update BackGoods : {}", backGoodsDTO);
        if (backGoodsDTO.getId() == null) {
            return createBackGoods(backGoodsDTO);
        }
        BackGoodsDTO result = backGoodsService.save(backGoodsDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("backGoods", backGoodsDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /back-goods : get all the backGoods.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of backGoods in body
     */
    @GetMapping("/back-goods")
    @Timed
    public List<BackGoodsDTO> getAllBackGoods() {
        log.debug("REST request to get all BackGoods");
        return backGoodsService.findAll();
    }

    /**
     * GET  /back-goods/:id : get the "id" backGoods.
     *
     * @param id the id of the backGoodsDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the backGoodsDTO, or with status 404 (Not Found)
     */
    @GetMapping("/back-goods/{id}")
    @Timed
    public ResponseEntity<BackGoodsDTO> getBackGoods(@PathVariable Long id) {
        log.debug("REST request to get BackGoods : {}", id);
        BackGoodsDTO backGoodsDTO = backGoodsService.findOne(id);
        return Optional.ofNullable(backGoodsDTO)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /back-goods/:id : delete the "id" backGoods.
     *
     * @param id the id of the backGoodsDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/back-goods/{id}")
    @Timed
    public ResponseEntity<Void> deleteBackGoods(@PathVariable Long id) {
        log.debug("REST request to delete BackGoods : {}", id);
        backGoodsService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("backGoods", id.toString())).build();
    }

    /**
     * SEARCH  /_search/back-goods?query=:query : search for the backGoods corresponding
     * to the query.
     *
     * @param query the query of the backGoods search 
     * @return the result of the search
     */
    @GetMapping("/_search/back-goods")
    @Timed
    public List<BackGoodsDTO> searchBackGoods(@RequestParam String query) {
        log.debug("REST request to search BackGoods for query {}", query);
        return backGoodsService.search(query);
    }


}
